A set of scripting commands for manipulating numbers.
Written by:
Blake Ward
18620 Favre Ridge Road
Los Gatos, CA
95030
Blake.Ward@alumni.cs.cmu.edu
or
bward@kagi.com
To use these scripting commands, just place the enclosed file ("Bits & Bytes Commands") in the Scripting Additions folder inside the Extensions folder in the System Folder. The commands are described below, but should be very self-explanatory. If you're unsure how a command works, just try it with some simple values and verify the result.
Since AppleScript treats all integers as signed values, the numbers you manipulate with these scripting commands will sometimes display as negative numbers or even numbers with an exponent. The numbers might look strange in AppleScript, but they are still correct at the bit level. For example:
set x to 1
set y to setBit 31 of x to 1
-- y looks like a negative number now, but 'getBit 31 of y' will return 1
-- 'getBit 0 of y' is also 1, getBit for all other bits will return 0
set z to decimal equivalent of "80000001"
-- y and z are now equal
set y to setBit 31 of y to 0
-- y is now equal to x again
Bits&Bytes was primarily written to make it easy to manipulate byte values for use with the SerialPort scripting addition. If you're interested in controlling devices that plug into the serial port from AppleScript, check out this scripting addition. It should be available from the same archive where you got Bits&Bytes.
This scripting addition is free for non-commercial use, you may include it with any scripts you write provided you give credit in the About Box and manual. If you want to use this scripting addition in a commercial product, please contact me for a very reasonable redistribution agreement.
Bits&Bytes Dictionary
bitwiseOR: Returns the bitwise OR of two numbers.
bitwiseOR integer -- first number to be ORed with the second number.
with integer -- second number to be ORed with the first number.
Result: integer -- the result of ORing the bits in the two arguments (e.g. 'bitwiseOR 3 with 6' returns 7).
bitwiseAND: Returns the bitwise AND of two numbers.
bitwiseAND integer -- first number to be ANDed with the second number
with integer -- second number to be ANDed with the first number
Result: integer -- the result of ANDing the bits in the two arguments (e.g. 'bitwiseAND 3 with 6' returns 2).
bitwiseXOR: Returns the XOR of the bits in the two arguments.
bitwiseXOR integer -- first number to XOR
with integer -- second number to XOR
Result: integer -- the bitwise XOR of the two arguments (e.g. 'bitwiseXOR 3 with 6' will return 5)
bitwiseNOT: Returns the one's complement of the argument.
bitwiseNOT integer -- number to one's complement
Result: integer
left shift: Shift all of the bits in the argument left by the given number of positions.
left shift integer -- number to be left shifted
by integer -- number of bits to shift the number left
Result: integer -- result of the left shift
right shift: Right shift the bits in the argument by the given number of positions.
right shift integer -- number to be bit shifted
by integer -- number of bits to shift the argument right
Result: integer -- result of shifting the argument right by the number of bits
hex equivalent of: Returns the hexadecimal equivalent of the given number.
hex equivalent of integer -- number to convert to a hex string
Result: string -- hexadecimal equivalent of the argument. If the argument is 0 - 255, then a two character string is returned. If 256 - 65535, a four character string is returned. Otherwise an eight character string is returned.
setBit: Set the given bit (0-31) of the value to a '0' or '1' and return the new value.
setBit integer -- which bit to change (where 31 is the most significant bit and 0 is the least significant bit).
of integer -- value to be changed
to integer -- new value for the given bit ('0' or '1').
Result: integer -- the new value with the given bit set or cleared.
getBit: Returns the value of the given bit of the value.
getBit integer -- which bit to return (where 31 is the most significant bit and 0 is the least significant bit).
of integer -- value to get the bit from.
Result: integer -- The current value of the given bit ('0' or '1').
decimal equivalent of: Returns the decimal equivalent of a hexidecimal string.